<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="generator" content="VuePress 2.0.0-beta.27">
    <link rel="icon" href="favicon.ico"><meta name="keywords" content="Cosla前端,Cosla开发,Cosla文档,酷斯啦前端,酷斯啦开发,酷斯啦文档"><title>开发约定 | 前端团队开发文档</title><meta name="description" content="Cosla开发，Cosla前端团队开发文档">
    <link rel="preload" href="/assets/js/runtime~app.7eeb5ba6.js" as="script"><link rel="preload" href="/assets/css/styles.a0dfb1ca.css" as="style"><link rel="preload" href="/assets/js/6135.d16fec79.js" as="script"><link rel="preload" href="/assets/js/app.4602c005.js" as="script">
    <link rel="stylesheet" href="/assets/css/styles.a0dfb1ca.css">
  </head>
  <body>
    <div id="app"><!--[--><div class="theme-container"><!--[--><header class="navbar"><div class="toggle-sidebar-button" title="toggle sidebar" aria-expanded="false" role="button" tabindex="0"><div class="icon" aria-hidden="true"><span></span><span></span><span></span></div></div><span><a href="/" class=""><!----><span class="site-name can-hide">前端团队开发文档</span></a></span><div class="navbar-links-wrapper" style=""><!--[--><!--]--><nav class="navbar-links can-hide"><!--[--><div class="navbar-links-item"><a href="/pages/resourcePack/" class="nav-link" aria-label="资源包"><!--[--><!--]--> 资源包 <!--[--><!--]--></a></div><div class="navbar-links-item"><a href="/pages/knowledge/developmentNorm/" class="nav-link" aria-label="知识库"><!--[--><!--]--> 知识库 <!--[--><!--]--></a></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="开发事项"><span class="title">开发事项</span><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="开发事项"><span class="title">开发事项</span><span class="right arrow"></span></button><!--[--><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/pages/developAttention/vehicleWebClient/" class="nav-link" aria-label="网页端"><!--[--><!--]--> 网页端 <!--[--><!--]--></a></li><li class="dropdown-item"><a href="/pages/developAttention/vehicleMiniprogram/" class="nav-link" aria-label="小程序端"><!--[--><!--]--> 小程序端 <!--[--><!--]--></a></li><!--]--></ul><!--]--></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="自定义组件"><span class="title">自定义组件</span><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="自定义组件"><span class="title">自定义组件</span><span class="right arrow"></span></button><!--[--><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/pages/customComponents/vehicleWebClient/detailsLayout/" class="nav-link" aria-label="网页端"><!--[--><!--]--> 网页端 <!--[--><!--]--></a></li><li class="dropdown-item"><a href="/pages/customComponents/vehicleMiniprogram/plate/" class="nav-link" aria-label="小程序端"><!--[--><!--]--> 小程序端 <!--[--><!--]--></a></li><!--]--></ul><!--]--></div></div><!--]--></nav><!--[--><!--]--><button class="toggle-dark-button" title="toggle dark mode"><svg style="" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M16 12.005a4 4 0 1 1-4 4a4.005 4.005 0 0 1 4-4m0-2a6 6 0 1 0 6 6a6 6 0 0 0-6-6z" fill="currentColor"></path><path d="M5.394 6.813l1.414-1.415l3.506 3.506L8.9 10.318z" fill="currentColor"></path><path d="M2 15.005h5v2H2z" fill="currentColor"></path><path d="M5.394 25.197L8.9 21.691l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 25.005h2v5h-2z" fill="currentColor"></path><path d="M21.687 23.106l1.414-1.415l3.506 3.506l-1.414 1.414z" fill="currentColor"></path><path d="M25 15.005h5v2h-5z" fill="currentColor"></path><path d="M21.687 8.904l3.506-3.506l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 2.005h2v5h-2z" fill="currentColor"></path></svg><svg style="display:none;" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M13.502 5.414a15.075 15.075 0 0 0 11.594 18.194a11.113 11.113 0 0 1-7.975 3.39c-.138 0-.278.005-.418 0a11.094 11.094 0 0 1-3.2-21.584M14.98 3a1.002 1.002 0 0 0-.175.016a13.096 13.096 0 0 0 1.825 25.981c.164.006.328 0 .49 0a13.072 13.072 0 0 0 10.703-5.555a1.01 1.01 0 0 0-.783-1.565A13.08 13.08 0 0 1 15.89 4.38A1.015 1.015 0 0 0 14.98 3z" fill="currentColor"></path></svg></button><!----></div></header><!--]--><div class="sidebar-mask"></div><!--[--><aside class="sidebar"><nav class="navbar-links"><!--[--><div class="navbar-links-item"><a href="/pages/resourcePack/" class="nav-link" aria-label="资源包"><!--[--><!--]--> 资源包 <!--[--><!--]--></a></div><div class="navbar-links-item"><a href="/pages/knowledge/developmentNorm/" class="nav-link" aria-label="知识库"><!--[--><!--]--> 知识库 <!--[--><!--]--></a></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="开发事项"><span class="title">开发事项</span><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="开发事项"><span class="title">开发事项</span><span class="right arrow"></span></button><!--[--><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/pages/developAttention/vehicleWebClient/" class="nav-link" aria-label="网页端"><!--[--><!--]--> 网页端 <!--[--><!--]--></a></li><li class="dropdown-item"><a href="/pages/developAttention/vehicleMiniprogram/" class="nav-link" aria-label="小程序端"><!--[--><!--]--> 小程序端 <!--[--><!--]--></a></li><!--]--></ul><!--]--></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="自定义组件"><span class="title">自定义组件</span><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="自定义组件"><span class="title">自定义组件</span><span class="right arrow"></span></button><!--[--><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/pages/customComponents/vehicleWebClient/detailsLayout/" class="nav-link" aria-label="网页端"><!--[--><!--]--> 网页端 <!--[--><!--]--></a></li><li class="dropdown-item"><a href="/pages/customComponents/vehicleMiniprogram/plate/" class="nav-link" aria-label="小程序端"><!--[--><!--]--> 小程序端 <!--[--><!--]--></a></li><!--]--></ul><!--]--></div></div><!--]--></nav><!--[--><!--]--><ul class="sidebar-links"><!--[--><!--[--><a aria-current="page" href="/pages/knowledge/developmentNorm.html" class="router-link-active router-link-exact-active nav-link router-link-active sidebar-heading sidebar-item active" aria-label="开发约定"><!--[--><!--]--> 开发约定 <!--[--><!--]--></a><ul class=""><li><!--[--><a aria-current="page" href="/pages/knowledge/developmentNorm.html#命名" class="router-link-active router-link-exact-active nav-link sidebar-item" aria-label="命名"><!--[--><!--]--> 命名 <!--[--><!--]--></a><!----><!--]--></li><li><!--[--><a aria-current="page" href="/pages/knowledge/developmentNorm.html#vue" class="router-link-active router-link-exact-active nav-link sidebar-item" aria-label="Vue"><!--[--><!--]--> Vue <!--[--><!--]--></a><!----><!--]--></li><li><!--[--><a aria-current="page" href="/pages/knowledge/developmentNorm.html#代码片段" class="router-link-active router-link-exact-active nav-link sidebar-item" aria-label="代码片段"><!--[--><!--]--> 代码片段 <!--[--><!--]--></a><!----><!--]--></li></ul><!--]--><!--[--><a href="/pages/knowledge/commitNorm.html" class="nav-link sidebar-heading sidebar-item" aria-label="git 提交约定"><!--[--><!--]--> git 提交约定 <!--[--><!--]--></a><!----><!--]--><!--[--><a href="/pages/knowledge/Vue3.html" class="nav-link sidebar-heading sidebar-item" aria-label="Vue3常用新特性"><!--[--><!--]--> Vue3常用新特性 <!--[--><!--]--></a><!----><!--]--><!--[--><a href="/pages/knowledge/codeOptimization.html" class="nav-link sidebar-heading sidebar-item" aria-label="代码优化"><!--[--><!--]--> 代码优化 <!--[--><!--]--></a><!----><!--]--><!--[--><a href="/pages/knowledge/recommendedBook.html" class="nav-link sidebar-heading sidebar-item" aria-label="推荐书籍/视频"><!--[--><!--]--> 推荐书籍/视频 <!--[--><!--]--></a><!----><!--]--><!--[--><a href="/pages/knowledge/startMac.html" class="nav-link sidebar-heading sidebar-item" aria-label="mac入门"><!--[--><!--]--> mac入门 <!--[--><!--]--></a><!----><!--]--><!--[--><a href="/pages/knowledge/vscodeHotkey.html" class="nav-link sidebar-heading sidebar-item" aria-label="vscode快捷键"><!--[--><!--]--> vscode快捷键 <!--[--><!--]--></a><!----><!--]--><!--[--><a href="/pages/knowledge/es6.html" class="nav-link sidebar-heading sidebar-item" aria-label="ES6使用技巧"><!--[--><!--]--> ES6使用技巧 <!--[--><!--]--></a><!----><!--]--><!--]--></ul><!--[--><!--]--></aside><!--]--><!--[--><main class="page"><!--[--><!--]--><div class="theme-default-content"><!--[--><h1 id="开发约定" tabindex="-1"><a class="header-anchor" href="#开发约定" aria-hidden="true">#</a> 开发约定</h1><h2 id="命名" tabindex="-1"><a class="header-anchor" href="#命名" aria-hidden="true">#</a> 命名</h2><ul><li><p>普通命名采用小驼峰式命名</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> userName <span class="token operator">=</span> <span class="token string">&#39;Jerrry&#39;</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div></li><li><p>复数的时候需要加 s</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> userNames <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&#39;Jerrry&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;Tom&#39;</span><span class="token punctuation">]</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div></li><li><p>每个常量都需命名，且名称全大写</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">COL_NUM</span> <span class="token operator">=</span> <span class="token number">10</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div></li><li><p>命名语义化</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// good</span>
<span class="token keyword">const</span> userDetails <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token comment">// not good</span>
<span class="token keyword">const</span> item <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li><li><p>函数命名，采用加上动词前缀</p><ul><li>can 判断是否可执行某个动作</li><li>has 判断是否含有某个值</li><li>is 判断是否为某个值</li><li>get 获取某个值</li><li>set 设置某个值</li></ul></li></ul><h2 id="vue" tabindex="-1"><a class="header-anchor" href="#vue" aria-hidden="true">#</a> Vue</h2><ul><li><p>自定义组件名使用大驼峰</p><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>如：LeftBar
</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div></li><li><p>props尽量定义详细</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// good</span>
props<span class="token operator">:</span> <span class="token punctuation">{</span>
  status<span class="token operator">:</span> String
<span class="token punctuation">}</span>

<span class="token comment">// not good</span>
props<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&#39;status&#39;</span><span class="token punctuation">]</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li><li><p>v-for 遍历必须添加 key（避免使用index作为key）</p><div class="language-html ext-html line-numbers-mode"><pre class="language-html"><code><span class="token comment">&lt;!-- good --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">v-for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>todo in todos<span class="token punctuation">&quot;</span></span> <span class="token attr-name">:key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>todo.id<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>{{ todo.text }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>

<span class="token comment">&lt;!-- not good --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">v-for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>todo in todos<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>{{ todo.text }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li><li><p>组件内样式设置作用域或添加顶级作用域</p><div class="language-scss ext-scss line-numbers-mode"><pre class="language-scss"><code><span class="token comment">/* good  */</span>
<span class="token selector">&lt;style scoped&gt;
.button-close </span><span class="token punctuation">{</span>
  <span class="token property">background-color</span><span class="token punctuation">:</span> #ccc<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">&lt;/style&gt;

&lt;style&gt;
.homepage </span><span class="token punctuation">{</span>
    <span class="token selector">.btn-close </span><span class="token punctuation">{</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span> #ccc<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
&lt;/style&gt;

<span class="token comment">/* not good  */</span>
<span class="token selector">&lt;style&gt;
.btn-close </span><span class="token punctuation">{</span>
  <span class="token property">background-color</span><span class="token punctuation">:</span> #ccc<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
&lt;/style&gt;
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div></li></ul><h2 id="代码片段" tabindex="-1"><a class="header-anchor" href="#代码片段" aria-hidden="true">#</a> 代码片段</h2><ul><li><p>用字面量赋值</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// good</span>
<span class="token keyword">const</span> userNames <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&#39;Jerrry&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;Tom&#39;</span><span class="token punctuation">]</span>

<span class="token comment">// not good</span>
<span class="token keyword">const</span> userNames <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token string">&#39;Jerrry&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;Tom&#39;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li><li><p>用扩展运算符做浅拷贝</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// good</span>
<span class="token keyword">const</span> userNames <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&#39;Jerrry&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;Tom&#39;</span><span class="token punctuation">]</span>
<span class="token keyword">const</span> copyUserNames <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token operator">...</span>userNames<span class="token punctuation">]</span>

<span class="token comment">// not good</span>
<span class="token keyword">const</span> userNames <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&#39;Jerrry&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;Tom&#39;</span><span class="token punctuation">]</span>
<span class="token keyword">const</span> copyUserNames <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> len <span class="token operator">=</span> userNames<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  copyUserNames<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> userNames<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
<span class="token punctuation">}</span>


<span class="token comment">// good</span>
<span class="token keyword">const</span> sources <span class="token operator">=</span> <span class="token punctuation">{</span>name<span class="token operator">:</span> <span class="token string">&#39;Jerry&#39;</span><span class="token punctuation">,</span> age<span class="token operator">:</span> <span class="token number">18</span><span class="token punctuation">}</span>
<span class="token keyword">const</span> target <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token operator">...</span>sources<span class="token punctuation">,</span> sex<span class="token operator">:</span> <span class="token string">&#39;男&#39;</span><span class="token punctuation">}</span> <span class="token comment">// 若sources中有sex字段，则会自动覆盖sources中的sex</span>

<span class="token comment">// not good</span>
<span class="token keyword">const</span> sources <span class="token operator">=</span> <span class="token punctuation">{</span>name<span class="token operator">:</span> <span class="token string">&#39;Jerry&#39;</span><span class="token punctuation">,</span> age<span class="token operator">:</span> <span class="token number">18</span><span class="token punctuation">}</span>
<span class="token keyword">const</span> target <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> sources<span class="token punctuation">,</span> <span class="token punctuation">{</span> sex<span class="token operator">:</span> <span class="token string">&#39;男&#39;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div></li><li><p>使用解构赋值</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// good</span>
<span class="token keyword">const</span> userNames <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&#39;Jerrry&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;Tom&#39;</span><span class="token punctuation">]</span>
<span class="token keyword">const</span> <span class="token punctuation">[</span>firstName<span class="token punctuation">,</span> secondName<span class="token punctuation">]</span> <span class="token operator">=</span> userNames

<span class="token comment">// not good</span>
<span class="token keyword">const</span> userNames <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&#39;Jerrry&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;Tom&#39;</span><span class="token punctuation">]</span>
<span class="token keyword">const</span> firstName <span class="token operator">=</span> userNames<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
<span class="token keyword">const</span> secondName <span class="token operator">=</span> userNames<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>

<span class="token comment">// good</span>
<span class="token keyword">const</span> person <span class="token operator">=</span> <span class="token punctuation">{</span>name<span class="token operator">:</span> <span class="token string">&#39;Jerry&#39;</span><span class="token punctuation">,</span> age<span class="token operator">:</span> <span class="token number">18</span><span class="token punctuation">}</span>
<span class="token keyword">const</span> <span class="token punctuation">{</span>name<span class="token punctuation">,</span> age<span class="token punctuation">}</span> <span class="token operator">=</span> person
<span class="token comment">// 使用别名</span>
<span class="token keyword">const</span> <span class="token punctuation">{</span>name<span class="token operator">:</span> userName<span class="token punctuation">,</span> age<span class="token operator">:</span> userAge<span class="token punctuation">}</span> <span class="token operator">=</span> person

<span class="token comment">// not good</span>
<span class="token keyword">const</span> person <span class="token operator">=</span> <span class="token punctuation">{</span>name<span class="token operator">:</span> <span class="token string">&#39;Jerry&#39;</span><span class="token punctuation">,</span> age<span class="token operator">:</span> <span class="token number">18</span><span class="token punctuation">}</span>
<span class="token keyword">const</span> name <span class="token operator">=</span> person<span class="token punctuation">.</span>name
<span class="token keyword">const</span> age <span class="token operator">=</span> person<span class="token punctuation">.</span>age
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div></li><li><p>对象使用属性缩写</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// good</span>
<span class="token keyword">const</span> name <span class="token operator">=</span> <span class="token string">&#39;Jerry&#39;</span>
<span class="token keyword">const</span> age <span class="token operator">=</span> <span class="token number">18</span>
<span class="token keyword">const</span> person <span class="token operator">=</span> <span class="token punctuation">{</span>name<span class="token punctuation">,</span> age<span class="token punctuation">,</span> sex<span class="token operator">:</span><span class="token string">&#39;男&#39;</span><span class="token punctuation">}</span> <span class="token comment">// 属性缩写放置在前面</span>

<span class="token comment">// not good</span>
<span class="token keyword">const</span> name <span class="token operator">=</span> <span class="token string">&#39;Jerry&#39;</span>
<span class="token keyword">const</span> age <span class="token operator">=</span> <span class="token number">18</span>
<span class="token keyword">const</span> person <span class="token operator">=</span> <span class="token punctuation">{</span>name<span class="token operator">:</span> name<span class="token punctuation">,</span> age<span class="token operator">:</span> age<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li><li><p>函数参数使用默认值</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// good</span>
<span class="token keyword">function</span> <span class="token function">createPerson</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">&#39;Jack&#39;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token operator">...</span>
<span class="token punctuation">}</span>

<span class="token comment">// not good</span>
<span class="token keyword">function</span> <span class="token function">createPerson</span><span class="token punctuation">(</span><span class="token parameter">name</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  name <span class="token operator">=</span> name <span class="token operator">||</span> <span class="token string">&#39;Jack&#39;</span>
   <span class="token operator">...</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></li><li><p>函数参数超过两个时使用对象结构</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// good</span>
<span class="token keyword">function</span> <span class="token function">createDialog</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span>title<span class="token punctuation">,</span> body<span class="token punctuation">,</span> buttonText<span class="token punctuation">,</span> cancelText<span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token operator">...</span>
<span class="token punctuation">}</span>

<span class="token function">createDialog</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  title<span class="token operator">:</span> <span class="token string">&#39;确认弹窗&#39;</span><span class="token punctuation">,</span>
  body<span class="token operator">:</span> <span class="token string">&#39;是否确认删除？&#39;</span><span class="token punctuation">,</span>
  buttonText<span class="token operator">:</span> <span class="token string">&#39;确认&#39;</span><span class="token punctuation">,</span>
  cancelText<span class="token operator">:</span> <span class="token string">&#39;取消&#39;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// not good</span>
<span class="token keyword">function</span> <span class="token function">createDialog</span><span class="token punctuation">(</span><span class="token parameter">title<span class="token punctuation">,</span> body<span class="token punctuation">,</span> buttonText<span class="token punctuation">,</span> cancelText</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// ...</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div></li><li><p>for循环，数组的长度，使用一个变量来接收</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// good</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> len <span class="token operator">=</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token operator">...</span>
<span class="token punctuation">}</span>

<span class="token comment">// not good</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token operator">...</span>
<span class="token punctuation">}</span>
<span class="token comment">// 该方式的缺点在于当循环次数较多时，循环条件i &lt; arr.length会多次arr.length造成性能损耗</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></li></ul><!--]--></div><footer class="page-meta"><!----><div class="meta-item last-updated"><span class="meta-item-label">上次更新: </span><span class="meta-item-info">2021-12-3 12:40:54</span></div><div class="meta-item contributors"><span class="meta-item-label">Contributors: </span><span class="meta-item-info"><!--[--><!--[--><span class="contributor" title="email: 1928537900@qq.com">jinleiFu</span><!----><!--]--><!--]--></span></div></footer><nav class="page-nav"><p class="inner"><!----><span class="next"><a href="/pages/knowledge/commitNorm.html" class="nav-link" aria-label="git 提交约定"><!--[--><!--]--> git 提交约定 <!--[--><!--]--></a> → </span></p></nav><!--[--><!--]--></main><!--]--></div><!----><!--]--></div>
    <script src="/assets/js/runtime~app.7eeb5ba6.js" defer></script><script src="/assets/js/6135.d16fec79.js" defer></script><script src="/assets/js/app.4602c005.js" defer></script>
  </body>
</html>
